
//HOC 高阶组件之操作props，属性代理
import React from 'react'
import Validate from '../../core/validate'
export default  function (file,Comp) {
    return class formProvider extends React.Component{
        constructor(props){
            super(props)
            this.state = {
                form:file,
                formValid: false // 加了一个formValid用来保存整个表单的校验状态
            };
            this.handleChange=this.handleChange.bind(this)
        }
        handleChange(fileName,value){
            let file=this.state.form;

            if(Validate.dataType[file[fileName].fileName].test(value)){
                file[fileName].msg='';
            }else{
                file[fileName].msg=value===''?'':Validate.msg[file[fileName].fileName];
            }
            this.setState({
                form:file
            })
        }
        render() {
            const {form,formValid} = this.state;
            return(
                <Comp {...this.props} form={form} formValid={formValid} onChange={this.handleChange} />
            )
        }
    }
}
